package com.xinjiang.electricpower.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.xinjiang.electricpower.domain.DTO.WorkOrderEarlyWarningDTO;
import com.xinjiang.electricpower.domain.DTO.WorkOrderMonitorDTO;
import com.xinjiang.electricpower.domain.vo.*;
import com.xinjiang.electricpower.service.IWorkOrderMonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author feizhao
 * @date 2022/11/11
 * @apiNote 工单监控控制层
 */
@RestController
@RequestMapping("/xinjiang-power-supply-station/work-order-Monitor/")
@Api(value = "工单中心-工单监控", tags = "工单中心-工单监控")
@Validated//有此注解，才会校验游离的参数
@Slf4j
@RequiredArgsConstructor
public class WorkOrderMonitorController extends BaseController {

    private final IWorkOrderMonitorService workOrderMonitorService;
    /**
     * @return R<Long> 工单总数
     * @description 统计当月的各个状态的工单数
     */
    @GetMapping("/monthly-work-order-statistics")
    @ApiOperation(value = "统计当月的各个状态的工单数", notes = "统计当月的各个状态的工单数")
    public R<List<WorkOrderStatisticsStatusVO>>  getWorkOrderPoolSummaryNum() {
        List<WorkOrderStatisticsStatusVO>  items=workOrderMonitorService.getNumsByStatus();
        return R.ok(items, "查询成功");
    }

    /**
     * @return R<List<WorkOrderStatisticsStatusVO>> 各个系统工单的待办状态总数
     * @description 统计待办工单每种工单类型的数量
     */
    @GetMapping("/monthly-system-work-order-statistics")
    @ApiOperation(value = "统计待办工单每种工单类型的数量", notes = "统计待办工单每种工单类型的数量")
    public R<List<SystemWorkOrderStatisticsStatusVO>> getSystemWorkOrderPoolSummaryNum() {
        List<SystemWorkOrderStatisticsStatusVO>  items=workOrderMonitorService.getNumsBySystemWorkOrderStatus();
        return R.ok(items, "查询成功");
    }

    /**
     * 工单监控-上方菜单栏系统数对不上
     * @param status
     * @return
     */
    @GetMapping("/count-system-orders")
    @ApiOperation(value = "查询各个系统下的工单总数", notes = "查询各个系统下的工单总数")
    public R<List<SystemOrdersVO>> getSystemOrderNum(String status) {
        return R.ok(workOrderMonitorService.countSystemOrders(status), "查询成功");
    }
    /**
     * @param workOrderMonitorDTO 工单监控实体类
     * @param page                     分页对象
     * @return R<IPage <WorkOrderMonitorVO>> 工单监控列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询工单监控的工单", notes = "查询工单监控的工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderEarlyWarningDTO", value = "工单监控DTO类", paramType = "query", dataType = "WorkOrderEarlyWarningDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.WorkOrderEarlyWarningDTO.class),
            @ApiImplicitParam(name = "page", value = "分页对象", paramType = "query", dataType = "PageDTO<XjOrderGeneralVO>", dataTypeClass = com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO.class)
    })
    public R<PageInfo<WorkOrderMonitorVO>> getXjOrderGeneralProcess(WorkOrderMonitorDTO workOrderMonitorDTO, PageDTO<WorkOrderMonitorVO> page) {
        PageInfo<WorkOrderMonitorVO> pages =workOrderMonitorService.selectWorkOrderMonitorPage(page, workOrderMonitorDTO);
        return R.ok(pages, "查询成功");
    }

    /**
     * @return R<Long> 工单总数
     * @description 工单预警按照各个类型汇总工单
     */
    @GetMapping("/count-status")
    @ApiOperation(value = "工单预警按照各个类型汇总工单", notes = "工单预警按照各个类型汇总工单")
    public R<Map<String ,Object>>

    getWorkOrderPoolSummaryNum(WorkOrderEarlyWarningDTO workOrderEarlyWarningDTO) {
        List<WorkOrderPoolSummaryVO> items = workOrderMonitorService.getXjOrderGeneralByStatus(workOrderEarlyWarningDTO);
        AtomicReference<Long> sum = new AtomicReference<>(0L);
        items.forEach(item ->{
            sum.updateAndGet(v -> v + item.getNum());
        });
        Map<String ,Object> map = new HashMap<>();
        map.put("sum",sum.get());
        map.put("items",items);
        return R.ok(map, "查询成功");
    }
    /**
     * 导出工单监控数据
     */
    @ApiOperation("导出工单监控数据")
    @Log(title = "导出工单监控数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderMonitorDTO", value = "workOrderMonitorDTO类的ids属性", required = true, paramType = "body", dataType = "WorkOrderMonitorDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.WorkOrderMonitorDTO.class),
            @ApiImplicitParam(name = "response", value = "响应流", dataType = "HttpServletResponse", dataTypeClass = javax.servlet.http.HttpServletResponse.class)
    })
    public void export(HttpServletResponse response, @RequestBody WorkOrderMonitorDTO workOrderMonitorDTO) {
        List<WorkOrderMonitorVO> list = workOrderMonitorService.selectWorkOrderMonitors(workOrderMonitorDTO.getIds());
        ExcelUtil<WorkOrderMonitorVO> util = new ExcelUtil<>(WorkOrderMonitorVO.class);
        util.exportExcel(response, list, "待处理、已完成、处理中的工单数据");
    }

    /**
     *
     */
    @ApiOperation("导出工单监控数据/全导出")
    @Log(title = "导出工单监控数据/全导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exports")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderMonitorDTO", value = "工单监控DTO实体类", paramType = "body", dataType = "WorkOrderMonitorDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.WorkOrderMonitorDTO.class),
            @ApiImplicitParam(name = "response", value = "响应流", dataType = "HttpServletResponse", dataTypeClass = javax.servlet.http.HttpServletResponse.class)
    })
    public void exports(HttpServletResponse response, @RequestBody WorkOrderMonitorDTO workOrderMonitorDTO) {

        List<WorkOrderMonitorVO> list = workOrderMonitorService.selectWorkOrderMonitors(workOrderMonitorDTO);
        ExcelUtil<WorkOrderMonitorVO> util = new ExcelUtil<>(WorkOrderMonitorVO.class);
        util.exportExcel(response, list, "导出预警工单数据");
    }
}
